﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ecomerci
{
    public partial class Ecomerci : Form
    {
        sqlite sql;
        applicationLogic logic;
        pdf pdf;

        public Ecomerci()
        {
            logic = new applicationLogic();
            logic.loadSettings();
            sql = new sqlite();
            pdf = new pdf();
            InitializeComponent();

            updateTransactionDatabase();
            fillTransactionMonthDetails();
            sbMonth.SelectedIndex = 0;
        }

        private void btAdd_transaction_Click(object sender, EventArgs e)
        {
            logic.callNewTransaction(); 
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            getAllDetails();
        }

        public void updateTransactionDatabase()
        {
            dataGridView1.DataSource = logic.getTransactionDatabase();
            dataGridView1.CurrentCell = dataGridView1[0, 0];
            getAllDetails();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = logic.getTransactionDatabase();
        }

        private void Ecomerci_FormClosing(object sender, FormClosingEventArgs e)
        {
            logic.createBackup();
        }

        private void getAllDetails()
        {
            transaction transaction = logic.getTransaction(int.Parse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString()));
            customer customer = logic.getCustomer(int.Parse(transaction.customer_id.ToString()));

            lbShowOffer_number.Text = transaction.offer_number.ToString();
            lbShowOffer_title.Text = transaction.offer_title.ToString();
            lbShowPrice.Text = transaction.price.ToString();
            lbShowShipping_id.Text = logic.getShipping(transaction.shipping.ToString());
            dateDate.SetDate(DateTime.Parse(transaction.date.ToString()));

            lbShowEbay_username.Text = customer.ebay_username;
            lbShowName.Text = customer.first_name + " " + customer.last_name;
            lbShowAdress.Text = customer.adress;
            lbShowArea_code.Text = customer.area_code.ToString();
            lbShowCity.Text = customer.city;
            lbShowEmail.Text = customer.email;
        }

        private void getCalcDetails()
        {
            transaction transaction = logic.getTransaction(int.Parse(dataGridView2.SelectedRows[0].Cells[1].Value.ToString()));
            customer customer = logic.getCustomer(int.Parse(transaction.customer_id.ToString()));

            lbCalcShowShipping.Text = logic.getShipping(transaction.shipping.ToString());
            lbCalcShowName.Text = customer.first_name + " " + customer.last_name;
            lbCalcShowAdress.Text = customer.adress;
            lbCalcShowArea_code_city.Text = customer.area_code + " " + customer.city;
            lbCalcShowEmail.Text = customer.email;
            lbCalcShowEbay.Text = customer.ebay_username;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            fillTransactionMonthDetails();
        }

        private void fillTransactionMonthDetails()
        {
            DataTable transactions = logic.getTransactionByMonth(logic.getDateRange(sbMonth.SelectedIndex, int.Parse(numYear.Value.ToString())));
            dataGridView2.DataSource = transactions;
            double price_accumulated = 0;
            double purchase_price_accumulated = 0;
            double tax = 0;

            foreach (DataRow row in transactions.Rows)
            {
                price_accumulated += double.Parse(row[3].ToString());
                purchase_price_accumulated += double.Parse(row[4].ToString());
            }

            tax = Math.Round((price_accumulated * 0.19), 2);
            lbCalcShowPurchase_price.Text = "€ " + purchase_price_accumulated.ToString();
            lbCalcShowPrice.Text = "€ " + price_accumulated.ToString();
            lbCalcShowTax.Text = "€ " + tax.ToString();
            lbCalcShowProfit.Text = "€ " + (price_accumulated - purchase_price_accumulated).ToString();
        }

        private void sbMonth_SelectedValueChanged(object sender, EventArgs e)
        {
            fillTransactionMonthDetails();
        }

        private void numYear_ValueChanged(object sender, EventArgs e)
        {
            fillTransactionMonthDetails();
        }

        private void dataGridView2_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            getCalcDetails();
        }

        private void printSales_Click(object sender, EventArgs e)
        {
            string pdfFile = pdf.createSalesOverview(logic.getDateRange(sbMonth.SelectedIndex, int.Parse(numYear.Value.ToString())));
            logic.callPdf(pdfFile);
        }

        private void btPrintInvoice_Click(object sender, EventArgs e)
        {
            string pdfFile = pdf.createInvoice(int.Parse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString()));
            logic.callPdf(pdfFile);
        }

        private void einstellungenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            logic.callPreferences();
        }

        private void transactionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            logic.callNewTransaction();
        }

        private void beendenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void überEcomerciToolStripMenuItem_Click(object sender, EventArgs e)
        {
            logic.callAboutEcomerci();
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            logic.callNewTransaction(logic.getTransaction(int.Parse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString())));
        }
    }
}